#include "I2C.h"

#define AT24C02_ADDRESS 0xA0	//定义AT24C02的I2C地址

/*********************************************************************
	方法：向AT24C02芯片某地址写入一个字节
	参数：WordAddress要写入数据的地址
	参数：WordAddress要写入的数据
	返回值：无
*/
void AT24C02_WriteByte(unsigned char WordAddress, Data)
{
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);		//写入从机地址
	I2C_ReceiveAck();
	I2C_SendByte(WordAddress);			//写入字地址
	I2C_ReceiveAck();
	I2C_SendByte(Data);					//写入字节
	I2C_ReceiveAck();
	I2C_Stop();
}

/*********************************************************************
	方法：从AT24C02芯片某地址读出一个字节
	参数：WordAddress要读出数据的地址
	返回值：Data读出的数据
*/
unsigned char AT24C02_ReadByte(unsigned char WordAddress)
{
	unsigned char Data;
	
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS);		//写入从机地址
	I2C_ReceiveAck();
	I2C_SendByte(WordAddress);			//写入字地址
	I2C_ReceiveAck();
	
	I2C_Start();
	I2C_SendByte(AT24C02_ADDRESS+1);	//设置读取从机地址
	I2C_ReceiveAck();
	Data = I2C_ReceiveByte();			//接收字节内容
	I2C_SendAck(1);
	I2C_Stop();
	
	return Data;
}